package com.ramos.kuaixiao.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.githang.statusbar.StatusBarCompat;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.entity.ReturnInfo;
import com.ramos.kuaixiao.utils.SharedPreference;
import com.ramos.kuaixiao.utils.StringUtil;
import com.ramos.kuaixiao.utils.ToastUtils;
import com.ramos.kuaixiao.utils.Utils;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = "RegisterActivity";
    private Context mContext;
    private TextView mSendVerifyText;
    private TextView mRegisterNum;
    private TextView mVerifyCode;
    private TextView mRegisterNextText;
    private EventHandler mSMSEventHandler;

    public final static int SEND_SMS_REQUEST_SUCCESS = 100;
    public final static int VERIFY_SMS_SUCCESS = 101;
    public final static int UPLOAD_FAILE = 102;
    public final static int VERIFY_SMART_SMS_SUCCESS = 103;
    private String mLoginname = "";
    LinearLayout title_reback;
    private TextView tv_aboutus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mContext = this;
        initView();
    }

    private void initView() {
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.title), false);
        mContext = this;
        tv_aboutus = (TextView) findViewById(R.id.title_text);
        tv_aboutus.setText("短信验证");
        title_reback = (LinearLayout) findViewById(R.id.title_reback);
        title_reback.setOnClickListener(this);

        mSendVerifyText = (TextView) findViewById(R.id.bt_yzm);
        mRegisterNum = (TextView) findViewById(R.id.et_tegiste_number);
        mVerifyCode = (TextView) findViewById(R.id.et_sms_number);
        mRegisterNextText = (TextView) findViewById(R.id.bt_register);

        mRegisterNextText.setOnClickListener(this);
        mSendVerifyText.setOnClickListener(this);

        //把短信里的验证码提交到服务器
        mSMSEventHandler = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    /*boolean smart = (Boolean) data;
                    if (smart) {
                        //通过智能验证
                        SharedPreference.setPreferString("user_phonenumber", mLoginname);
                        sendMessagea(VERIFY_SMART_SMS_SUCCESS, result);
                    } else {*/
                    //依然走短信验证
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                        sendMessagea(VERIFY_SMS_SUCCESS, result);
                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        //获取验证码成功
                        //sendMessagea(SEND_SMS_REQUEST_SUCCESS, result);
                        //Log.d(TAG, result + "");
                        //System.out.println("获取验证码成功");
                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                        //返回支持发送验证码的国家列表
                    }
                    /*}*/

                } else {
                    sendMessagea(UPLOAD_FAILE, ((Throwable) data).getMessage());
                    ((Throwable) data).printStackTrace();
                }
            }
        };
        SMSSDK.registerEventHandler(mSMSEventHandler); //注册短信回调
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(mSMSEventHandler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_yzm:
                getVerityCode();
                break;
            case R.id.title_reback:
                finish();
                break;
            case R.id.bt_register:
                verifyRegisterInfo();
                //startActivity(new Intent(mContext, EditUserInfoActivity.class));
                break;
            default:
                break;
        }
    }

    private void verifyRegisterInfo() {
        mLoginname = mRegisterNum.getText().toString();
        String verfyCode = mVerifyCode.getText().toString();
        if (StringUtil.isEmpty(mLoginname) && Utils.isMobileNO(mLoginname)) {
            Toast.makeText(mContext, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtil.isEmpty(verfyCode)) {
            Toast.makeText(mContext, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            SMSSDK.submitVerificationCode("86", mLoginname, verfyCode);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void getVerityCode() {
        mLoginname = mRegisterNum.getText().toString();
        if (!(StringUtil.isEmpty(mLoginname)) && Utils.isMobileNO(mLoginname)) {
            try {
                SMSSDK.getVerificationCode("86", mLoginname);
                mRegisterNum.setEnabled(false);
                mSendVerifyText.setText("验证码已发送");
                mSendVerifyText.setEnabled(false);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            Toast.makeText(mContext, "请输入手机号", Toast.LENGTH_SHORT).show();
        }
    }

    private void sendMessagea(int iWhat, Object obj) {
        Message msg = Message.obtain();
        msg.what = iWhat;
        msg.obj = obj;
        mHandler.sendMessage(msg);
    }

    @SuppressLint("HandlerLeak")
    public Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case VERIFY_SMART_SMS_SUCCESS:
                    Toast.makeText(mContext, "号码已注册，可直接登录", Toast.LENGTH_LONG).show();
                    break;
                case VERIFY_SMS_SUCCESS:
                    String result = msg.obj.toString();
                    //Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
                    ToastUtils.show("短信验证成功");
                    /*ResultBean resultBean = null;*/
                    try {
                       /* resultBean = JSON.parseObject(result, ResultBean.class);
                        if (resultBean.getStatus() == 0) {*/
                        SharedPreference.setPreferString("user_phonenumber", mLoginname);
                        startActivity(new Intent(mContext, EditUserInfoActivity.class));
                        finish();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;

                case UPLOAD_FAILE:
                    String result_fail = msg.obj.toString();
                    ReturnInfo returnInfo = null;
                    try {
                        returnInfo = JSON.parseObject(result_fail, ReturnInfo.class);
                        mRegisterNum.setEnabled(true);
                        mSendVerifyText.setEnabled(true);
                        mSendVerifyText.setText("获取验证码");
                        //Toast.makeText(mContext, returnInfo.getDetail(), Toast.LENGTH_SHORT).show();
                        ToastUtils.show("短信验证失败");
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                    break;
                default:
                    break;
            }
        }
    };
}
